package com.epoint.flinkdemo.source;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author liufl
 * @version [版本号, 21-3-30]
 */
public class TestSocket
{
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> socketStream = env.socketTextStream("localhost", 7777);

        // wordCount统计
        // 先分组后聚合
        SingleOutputStreamOperator<Tuple2<String, Integer>> sumStream = socketStream.flatMap(new WordCountFlat())
                .keyBy((KeySelector<Tuple2<String, Integer>, String>) tuple2 -> tuple2.getField(0)).sum(1);

        sumStream.print();

        env.execute();
    }
}

